package com.ruoyi.web.controller.tdengine;

import com.ruoyi.data.tdengine.domain.AvgForTimeInterval;
import com.ruoyi.data.tdengine.domain.Sensor;
import com.ruoyi.data.tdengine.domain.SensorDataOfEquipment;
import com.ruoyi.data.tdengine.domain.ValueAvg;
import com.ruoyi.data.tdengine.service.SensorService;
import com.ruoyi.biz.service.IChipInfoService;
import com.ruoyi.biz.service.ISensorInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

/**
 * Created by james-YU on 2020/12/22
 * DESC:
 */
@RestController
@RequestMapping("/sensor")
@Api(tags = "传感器数据管理")
public class SensorController {

    @Autowired
    private SensorService sensorService;


    @Autowired
    private IChipInfoService chipInfoService;

    @Autowired
    private ISensorInfoService sensorInfoService;

    /**
     * 获取数据，渲染图表  查询过去一个月的数据，按一天来分析
     * @param sensorCode 超级表的具体子表表名
     */
    @ApiOperation("获取数据")
    @GetMapping("/data/{sensorCode}")
    public  List<Sensor> getData(@PathVariable String sensorCode){
        List<Sensor> sensorList  = sensorService.findTimeDataCount(sensorCode);
        return sensorList;
    }

    // 新增传感器  同时插入mysql和Tengine 保证事务
    @PostMapping("/add")
    @ApiOperation("新增传感器")
    public int add(@RequestBody Sensor sensor) {
        return sensorService.addSensor(sensor);
    }

    // 查询所有传感器数据
    @GetMapping("/listAll")
    @ApiOperation("查询所有传感器数据")
    public List<Sensor> listAll() {
        return sensorService.listAll();
    }

    // 获取指定传感器的指定时间段的指定时间间隔的value的平均值
    @ApiOperation("获取指定传感器的指定时间段的指定时间间隔的value的平均值")
    @PostMapping("/getAvgForTimeInterval")
    public List<ValueAvg>  getAvgForTimeInterval(@RequestBody AvgForTimeInterval avgForTimeInterval) {
        return sensorService.getAvgForTimeInterval(avgForTimeInterval);
    }
    // 新增传感器数据
    @ApiOperation("新增传感器数据")
    @PostMapping("/insertData")
    public int insertData(@RequestBody Sensor sensor) {
        return sensorService.insert(sensor);
    }

    // 新增传感器数据
    @ApiOperation("新增传感器数据")
    @PostMapping("/insertBatch")
    public int insertBatch() {
        Sensor sensor = new Sensor();
        sensor.setValue(23.5);
        sensor.setSensorCode("test0001");
        sensor.setEquipmentCode("8080-97-898");
        Timestamp createTime = new Timestamp(new Date().getTime());
        sensor.setTs(createTime);
        List<Sensor> sensors = new ArrayList<>();
        sensors.add(sensor);
        return sensorService.insertBatch(sensors);
    }

    // 获取指定设备下的所有的传感器在指定时间范围的数据列表
    @ApiOperation("获取指定设备下的所有的传感器在指定时间范围的数据列表")
    @PostMapping("/findSensorDataOfEquipment")
    public List<Sensor> findSensorDataOfEquipment(@RequestBody SensorDataOfEquipment sensorDataOfEquipment) {
        return sensorService.findSensorDataOfEquipment(sensorDataOfEquipment);
    }

    // 获取指定设备下的所有的传感器最新的数据
    @ApiOperation("获取指定设备下的所有的传感器最新的数据")
    @PostMapping("/findLastSensorData")
    public Sensor findLastSensorData(@RequestBody SensorDataOfEquipment sensorDataOfEquipment) {
        return sensorService.findLastSensorData(sensorDataOfEquipment);
    }

    // 获取指定设备下的所有的传感器最新的数据
    @PostMapping("/test")
    public Double test(@RequestBody SensorDataOfEquipment sensorDataOfEquipment) {
        return sensorService.getAvgForTimeLimit(sensorDataOfEquipment);
    }




}
